package data;

import java.sql.*; 
import java.text.SimpleDateFormat;
import java.util.*;
import beans.*;

/*
 * Author: Minh-Tuan Ta
 * Date: 11/06/11
 * Class: DB
 * This class will serve as a base class for all the data access classes
 */
public abstract class DB {
	
	/*
	 * Author: Minh-Tuan Ta
	 * Date: 11/6/11
	 * Method: getConnection
	 * This method returns the Connection object which is used to connect to the database
	 */
	protected static Connection getConnection()
	{
		 Connection conn;
		 
	        try {
	            conn = DriverManager.getConnection("jdbc:mysql://hightower2.it.uafortsmith.edu:3306/group3?user=group3&password=g1654d");
	        }
	        catch (SQLException ex) {
	            System.out.println("Error: " + ex);
	            return null;
	        }
	        catch(NullPointerException ex) {
	        	System.out.println("Error: " + ex);
	        	return null;
	        }
	        
	        return conn;		
	}
	
	/*
	 * Author: Minh-Tuan Ta
	 * Date: 11/6/11
	 * Method: setUser
	 * This method sets the User object using the information retreived from the database
	 */
	protected static void setUser(User user, ResultSet rs) throws SQLException
	{
		user.setUserid(rs.getInt("userid"));
		user.setUserName(rs.getString("username"));
		user.setFirstName(rs.getString("firstname"));
		user.setLastName(rs.getString("lastname"));
		user.setEmail(rs.getString("email"));
		user.setPassword(rs.getString("password"));
		user.setDob(rs.getDate("dob"));
		user.setGender(rs.getBoolean("gender"));
		user.setPhone(rs.getString("phone"));
		user.setAddress1(rs.getString("address1"));
		user.setAddress2(rs.getString("address2"));
		user.setCity(rs.getString("city"));
		user.setState(rs.getString("state"));
		user.setZip(rs.getString("zip"));
		user.setLastlogin(rs.getTimestamp("lastlogin"));
		user.setPicturePath(rs.getString("picturepath"));
	}
	
	/*
	 * Author: Minh-Tuan Ta
	 * Date: 11/6/11
	 * Method: setFriend
	 * This method sets the Friend object using the information retrieved from the database
	 */
	protected static void setFriend(Friend friend, ResultSet rs) throws SQLException
	{
		friend.setUserID(rs.getInt("userid"));
		friend.setFriendID(rs.getInt("friendid"));
	}
	
	/*
	 * Author: Minh-Tuan Ta
	 * Date: 11/6/11
	 * Method: setContentType
	 * This method sets the ContentType object using the information retrieved from the database
	 */
	protected static void setContentType(ContentType contenttype, ResultSet rs) throws SQLException
	{
		contenttype.setContentTypeID(rs.getInt("contenttypeid"));
		contenttype.setContentTypeName(rs.getString("contenttypename"));
		contenttype.setContentTypeDescription(rs.getString("contenttypedescription"));
	}
	
	/*
	 * Author: Minh-Tuan Ta
	 * Date: 11/6/11
	 * Method: setContent
	 * This method sets the Content object using the information retrieved from the database
	 */
	protected static void setContent(Content content, ResultSet rs) throws SQLException
	{
		content.setContentID(rs.getInt("contentid"));
		content.setContentTitle(rs.getString("contenttitle"));
		content.setContent(rs.getString("content"));
		content.setContentTypeID(rs.getInt("contenttypeid"));
		content.setDateCreated(rs.getTimestamp("datecreated"));
		content.setExtra(rs.getInt("extra"));
		content.setStatus(rs.getInt("status"));
		content.setUserID(rs.getInt("userid"));
		content.setUser(UserDB.getUser(content.getUserID()));
		content.setFormattedDateCreatedString(new SimpleDateFormat("MM/dd/yyyy hh:mm:ssa").format(content.getDateCreated()));
	}
	
	/*
	 * Author: Minh-Tuan Ta
	 * Date: 11/6/11
	 * Method: setHelpfulRating
	 * This method sets the HelpfulRating object using the information retrieved from the database
	 */
	protected static void setHelpfulRating(HelpfulRating hr, ResultSet rs) throws SQLException
	{
		hr.setContentID(rs.getInt("contentid"));
		hr.setRateValue(rs.getInt("rateValue"));
		hr.setRatingDate(rs.getDate("ratingdate"));
	}
	
	/*
	 * Author: Minh-Tuan Ta
	 * Date: 11/6/11
	 * Method: setResource
	 * This method sets the HelpfulRating object using the information retrieved from the database
	 */
	protected static void setResource(Resource resource, ResultSet rs) throws SQLException
	{
		resource.setContentID(rs.getInt("contentid"));
		resource.setPath(rs.getString("path"));
		resource.setResourceID(rs.getInt("resourceid"));
		resource.setResourceName(rs.getString("resourcename"));
	}
}
